package com.doctor.ui.consulting;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;

import com.doctor.bean.DoctorBean;
import com.doctor.comm.ConstConfig;
import com.doctor.comm.DialogHelper;
import com.doctor.comm.DialogHelper.DialogSelectCallback;
import com.doctor.database.DbOperator;
import com.doctor.ui.BaseFragment;
import com.doctor.ui.R;
import com.doctor.ui.consulting.view.ConsultingActivity;

public class ModifyDualDiagGroupFrag extends BaseFragment{

	/**
	 * 修改双诊群
	 */
	
	private Context context;
	private Calendar calendar;
	private EditText name,hospital,postTitle,partment,post,diagTime,address,phone,mobile,
	        specialNum,email,qq,blog,fetion,webChart,webSite,other;
	private RadioButton male,female;
	private TextView birthday,advantage;
	private LinearLayout saveBtn,cancelBtn;
	private Click click;
	private DoctorBean bean;
	private List<String> advantageNames;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		context=getActivity();
		calendar=Calendar.getInstance();
		click=new Click();
		bean=(DoctorBean) getArguments().getSerializable(ConstConfig.BEAN);
		
		View view=inflater.inflate(R.layout.modify_dual_diag_group, container, false);
		initiaView(view);
				
		return view;
	}
	
	@Override
	public void onDestroyView() {
		// TODO Auto-generated method stub
		super.onDestroyView();
//		context=null;
//		calendar=null;
//		click=null;
//		bean=null;
	}
	
	@Override
	public void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
	}	
	
	
	@Override
	protected void initiaView(View view) {
		// TODO Auto-generated method stub
		name=(EditText) view.findViewById(R.id.name);
		name.setText(bean.getRealname());
		
		hospital=(EditText) view.findViewById(R.id.hospital);
		hospital.setText(bean.getHospital());
		
		postTitle=(EditText) view.findViewById(R.id.postTitle);
		postTitle.setText(bean.getZc());
		
		partment=(EditText) view.findViewById(R.id.partment);
		partment.setText(bean.getKs());
		
		post=(EditText) view.findViewById(R.id.post);
		post.setText(bean.getZw());
		
		advantage=(TextView) view.findViewById(R.id.advantage);
		if (ConsultingActivity.map1.size()>0) {
			advantage.setText(ConsultingActivity.map1.get(bean.getExpertise()));
		}
		advantage.setOnClickListener(click);
		
		diagTime=(EditText) view.findViewById(R.id.diagTime);
		diagTime.setText(bean.getCztime());
		
		address=(EditText) view.findViewById(R.id.address);
		address.setText(bean.getAddress());
		
		phone=(EditText) view.findViewById(R.id.phone);
		phone.setText(bean.getTel());
		
		mobile=(EditText) view.findViewById(R.id.mobile);
		mobile.setText(bean.getMobile());
		
		specialNum=(EditText) view.findViewById(R.id.specialNum);
		specialNum.setText(bean.getZh());
		
		email=(EditText) view.findViewById(R.id.email);
		email.setText(bean.getEmail());
		
		qq=(EditText) view.findViewById(R.id.qq);
		qq.setText(bean.getQq());
		
		blog=(EditText) view.findViewById(R.id.blog);
		blog.setText(bean.getBlog());
		
		fetion=(EditText) view.findViewById(R.id.fetion);
		fetion.setText(bean.getFx());
		
		webChart=(EditText) view.findViewById(R.id.webChart);
		webChart.setText(bean.getWx());
		
		webSite=(EditText) view.findViewById(R.id.webSite);
		webSite.setText(bean.getWebsite());
		
		other=(EditText) view.findViewById(R.id.other);
		other.setText(bean.getRemark());
		
		male=(RadioButton) view.findViewById(R.id.male);
		female=(RadioButton) view.findViewById(R.id.female);
		String sex_txt=bean.getSex();
		if (sex_txt!=null && "男".equals(sex_txt.trim())) {
			male.setChecked(true);
		} else if (sex_txt!=null && "女".equals(sex_txt.trim())) {	
			female.setChecked(true);
		}
		
		birthday=(TextView) view.findViewById(R.id.birthday);
		birthday.setText(bean.getBrith());
		birthday.setOnClickListener(click);
		
		saveBtn=(LinearLayout) view.findViewById(R.id.saveBtn);
		saveBtn.setOnClickListener(click);
		
		cancelBtn=(LinearLayout) view.findViewById(R.id.cancelBtn);
		cancelBtn.setOnClickListener(click);
	}
	
	
	private class Click implements OnClickListener{
		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
		   if (v==advantage) {
			   if (advantageNames==null) {
					advantageNames=new ArrayList<String>();
					if (ConsultingActivity.map2.size()>0) {
						for (String name : ConsultingActivity.map2.keySet()) {
							advantageNames.add(name);
						}
					}
				}
				if (advantageNames.size()>0) {
					DialogHelper.selectDialog(context, "选择科目", advantageNames, new DialogSelectCallback() {						
						@Override
						public void onDialogSelected(int position) {
							// TODO Auto-generated method stub
							advantage.setText(advantageNames.get(position));
						}
					});
				}
		   } else if (v==birthday) {
			   DialogHelper.showDatePickerDialog(context, birthday, calendar, false);
			} else if (v==saveBtn) {
				String name_txt=name.getText().toString().trim();
				if (name_txt==null || "".equals(name_txt)) {
					Toast.makeText(context, "请填写姓名！", Toast.LENGTH_SHORT).show();
					return;
				}
				
				ContentValues values=new ContentValues();
				values.put("_name", name_txt);
				
				if (male.isChecked()) {
					values.put("_sex", "男");
				} else if (female.isChecked()) {
					values.put("_sex", "女");
				}
				
				String hospital_txt=hospital.getText().toString().trim();
				values.put("_hospital", hospital_txt);
				
				String birthday_txt=birthday.getText().toString();
				values.put("_birth_day", birthday_txt);
				
				String postTitle_txt=postTitle.getText().toString().trim();
				values.put("_post_title", postTitle_txt);
				
				String partment_txt=partment.getText().toString().trim();
				values.put("_partment", partment_txt);
				
				String post_txt=post.getText().toString().trim();
				values.put("_post", post_txt);
				
				String advantage_txt=advantage.getText().toString().trim();
				if (advantage_txt!=null && !"".equals(advantage_txt) && ConsultingActivity.map2.size()>0) {
					values.put("_advantage", ConsultingActivity.map2.get(advantage_txt));
				}
				
				String diagTime_txt=diagTime.getText().toString().trim();
				values.put("_time", diagTime_txt);
				
				String address_txt=address.getText().toString().trim();
				values.put("_address", address_txt);
				
				String phone_txt=phone.getText().toString().trim();
				values.put("_phone", phone_txt);
				
				String mobile_txt=mobile.getText().toString().trim();
				values.put("_mobile", mobile_txt);
				
				String specialNum_txt=specialNum.getText().toString().trim();
				values.put("_special_num", specialNum_txt);
				
				String email_txt=email.getText().toString().trim();
				values.put("_email", email_txt);
				
				String qq_txt=qq.getText().toString().trim();
				values.put("_qq", qq_txt);
				
				String blog_txt=blog.getText().toString().trim();
				values.put("_blog", blog_txt);
				
				String fetion_txt=fetion.getText().toString().trim();
				values.put("_fetion", fetion_txt);
				
				String webChart_txt=webChart.getText().toString().trim();
				values.put("_webchat", webChart_txt);
				
				String webSite_txt=webSite.getText().toString().trim();
				values.put("_webSite", webSite_txt);
				
				String other_txt=other.getText().toString().trim();
				values.put("_other", other_txt);
				
				int row=DbOperator.getInstance(context).updateData(ConstConfig.DOCTOR_TABLE, values, bean.getId());
				if (row>0) {
					Toast.makeText(context, "修改成功！", Toast.LENGTH_SHORT).show();
					getActivity().getSupportFragmentManager().popBackStack();
				}
				
			} else if (v==cancelBtn) {
				getActivity().getSupportFragmentManager().popBackStack();
			}
		}		
	}	
	
	
	
	
	
}
